﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IBM.Data.DB2;
using CourseManager.Domain.Entities;
using CourseManager.Domain.Repositories;

namespace CourseManager.WebUI.Controllers
{
    public class StudentController : Controller
    {
        //
        // GET: /Student/

        Student Student;
        StudentRepository SR;
        //
    
        [HttpGet]
        public ActionResult Index()
        {
            return View();

        }


        public ActionResult GetStudent(int pId)
        {
            SR = new StudentRepository();
            Student = SR.GetStudent(pId);
            return View(Student);
        }

        [HttpGet]
        public ActionResult GetStudentByName()
        {
            return View();
        
        }

        public ActionResult GetStudentByName(FormCollection pName)
        {
            if (pName["StudentName"].Trim().Length == 0)
            {
                ModelState.AddModelError("StudentName", "Requerido");
                return View();

            }

            IEnumerable<Student> StudentList;
            SR = new StudentRepository();
            StudentList = SR.GetStudent(pName["StudentName"].ToString());
            return View(StudentList);
        }

        [HttpGet]
        public ActionResult GetStudentByInterest()
        {
            return View();
        
        }

        public ActionResult GetStudentByInterest(FormCollection pInterest)
        {
            if (pInterest["Interest"].Trim().Length == 0)
            {
                ModelState.AddModelError("Interest", "Requerido");
                return View();

            }

            IEnumerable<Student> StudentList;
            SR = new StudentRepository();
            StudentList = SR.GetStudentByInterest(pInterest["Interest"].ToString());
            return View(StudentList);
        }

        [HttpGet]
        public ActionResult GetStudentByPosition()
        {
          
            return View();
        }

        [HttpPost]
        public ActionResult GetStudentByPosition(FormCollection pPosition)
        {
            if (pPosition["Position"].Trim().Length == 0)
            {
                ModelState.AddModelError("Position", "Requerido");
                return View();

            }

            IEnumerable<Student> StudentList;
            SR = new StudentRepository();
            StudentList = SR.GetStudentByPosition(pPosition["Position"]);
            return View(StudentList);
        }

        [HttpGet]
        public ActionResult AddStudent()
        {
            return View();
        
        }

        [HttpPost]
        public ActionResult AddStudent(FormCollection pStudent)
        {
            SR = new StudentRepository();
            Student Student = new Student();

            if (pStudent["Name"].Trim().Length == 0)
            {
                ModelState.AddModelError("Name", "Requerido");
                return View();
                
            }
            else if(pStudent["LastName"].Trim().Length == 0) 
            {
                ModelState.AddModelError("LastName", "Requerido");
                return View();
            
            }
            else if (pStudent["Telephone"].Trim().Length == 0)
            {
                ModelState.AddModelError("Telephone", "Requerido");
                return View();

            }
            else if (pStudent["Cellphone"].Trim().Length == 0)
            {
                ModelState.AddModelError("Cellphone", "Requerido");
                return View();

            }
            else if (pStudent["Email"].Trim().Length == 0)
            {
                ModelState.AddModelError("Email", "Requerido");
                return View();

            }
            else if (pStudent["Address"].Trim().Length == 0)
            {
                ModelState.AddModelError("Address", "Requerido");
                return View();

            }
            else if (pStudent["Position"].Trim().Length == 0)
            {
                ModelState.AddModelError("Position", "Requerido");
                return View();

            }
            else if (pStudent["Interest"].Trim().Length == 0)
            {
                ModelState.AddModelError("Interest", "Requerido");
                return View();

            }
            
            Student.Name = pStudent["Name"].ToString();
            Student.LastName = pStudent["LastName"].ToString();
            Student.Cellphone = pStudent["Cellphone"].ToString();
            Student.Email = pStudent["Email"].ToString();
            Student.Address = pStudent["Address"].ToString();
            Student.Telephone = pStudent["Telephone"].ToString();
            Student.Insterest = pStudent["Interest"].ToString();
            Student.Position = pStudent["Position"].ToString();
            SR.AddStudent(Student);

            return View();

        }

        public bool DeleteStudent(int pId)
        {
            SR = new StudentRepository();
            SR.DeleteStudent(pId);
            return true;
        }

        public ActionResult UpdateCourse(Student pStudent)
        {
            SR = new StudentRepository();
            SR.UpdateStudent(pStudent);
            return View();
        }

    }
}
